package com.canlihao.ui.mvp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.canlihao.ui.R;
import com.canlihao.ui.base.BaseApplication;
import com.canlihao.ui.utils.MyLog;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;

import java.io.ByteArrayOutputStream;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by QYG_XXY on 0007 2017/12/7.
 */
public class Dialogwx extends Dialog {
    private static final String TAG = "Dialogwx";
    Context context;
    @BindView(R.id.tv_top)
    TextView tvTop;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_dddd)
    RelativeLayout rlDddd;
    @BindView(R.id.rl_nnn)
    RelativeLayout rlNnn;
    @BindView(R.id.tv_diss)
    TextView tvDiss;

    public Dialogwx(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public Dialogwx(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_down);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        ButterKnife.bind(this);
        tvDiss.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                dismiss();
            }
        });
        rlDddd.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                dismiss();
            }
        });
        rlNnn.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                MyLog.i("", "");
            }
        });
        llLeft.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                WXWebpageObject webpage = new WXWebpageObject();
                webpage.webpageUrl = "https://datalw.wyolo.cn/html/ceshi.html";
                //https://datalw.wyolo.cn/html/ceshi.html
//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
                WXMediaMessage msg = new WXMediaMessage(webpage);
                msg.title = "雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页 ";
                msg.description = "JS测试雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页雷总的网页";
//                Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_test_3);
                Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.launch_logo);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                thumbBmp.compress(Bitmap.CompressFormat.PNG, 10, baos);
                msg.thumbData = baos.toByteArray();

//构造一个Req
                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTransaction("webpage");
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneSession;
                BaseApplication.getWxApi().sendReq(req);
                dismiss();
            }
        });
        llRight.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                WXWebpageObject webpage = new WXWebpageObject();
                webpage.webpageUrl = "https://datalw.wyolo.cn/html/ceshi.html";
                //https://datalw.wyolo.cn/html/ceshi.html
//用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
                WXMediaMessage msg = new WXMediaMessage(webpage);
                msg.title = "雷总的网页 ";
                msg.description = "JS测试";
                Bitmap thumbBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.launch_logo);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                thumbBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
                msg.thumbData = baos.toByteArray();

//构造一个Req
                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTransaction("webpage");
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneTimeline;
                BaseApplication.getWxApi().sendReq(req);
                dismiss();
            }
        });
    }

    private static String buildTransaction(String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    /**
     * 保证同一按钮在1秒内只会响应一次点击事件
     */
    public abstract class OnSingleClickListener implements View.OnClickListener {
        //两次点击按钮之间的间隔，目前为1000ms
        private static final int MIN_CLICK_DELAY_TIME = 1500;
        private long lastClickTime;

        public abstract void onSingleClick(View view);

        @Override
        public void onClick(View view) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                onSingleClick(view);
            }
        }
    }
}
